package de.uni.leipzig.meddins.server;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

import android.content.Context;
import android.util.Log;

public class DefaultHandler implements HttpRequestHandler {

	private Context context = null;

	public DefaultHandler(Context context) {
		this.setContext(context);
	}

	public void handle(HttpRequest request, HttpResponse response,
			HttpContext httpContext) throws HttpException, IOException {
		Log.i(DefaultHandler.class.getName(), "Handle default request");
		HttpEntity entity = null;
		entity = new EntityTemplate(new ContentProducer() {
			public void writeTo(final OutputStream outstream)
					throws IOException {
				OutputStreamWriter writer = new OutputStreamWriter(outstream,
						"UTF-8");
				String resp = "Welcome to the WebSocket Server. If u want to use WebSockets go to <server>/websocket/<application>";
				writer.write(resp);
				writer.flush();
			}
		});
		response.setHeader("Context-Type", "text/html");
		response.setEntity(entity);
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
